//
//  HSLeftViewController.swift
//  Home_school_晓林
//
//  Created by 张晓林 on 16/9/7.
//  Copyright © 2016年 张晓林. All rights reserved.
//

import UIKit
import SnapKit

class HSLeftViewController: UIViewController {
    @IBOutlet weak var iocnImageVHeight: NSLayoutConstraint!
    @IBOutlet weak var nameLMargin: NSLayoutConstraint!
    
    @IBOutlet weak var leftUserInfoBGView: UIView!
    @IBOutlet weak var classIconImageV: UIImageView!
    @IBOutlet weak var leftNameL: UILabel!
    @IBOutlet weak var classAccountL: UILabel!
    @IBOutlet weak var classNameL: UILabel!
    @IBOutlet weak var schoolNameL: UILabel!
    @IBOutlet weak var userScoreL: UILabel!
    @IBOutlet weak var scoreView: UIView!
    @IBOutlet var leftButtonArray: [UIButton]!

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    /// 左边按钮点击回调
    @IBAction fileprivate func leftViewClicked(_ sender: UIButton) {
        switch sender.currentTitle! {
        case "课程表":
            print("点击了课程表")
            let classTableVc = HSClassTableController()
            present(classTableVc, animated: true, completion: nil)
        case "关于我们":
            print("点击了关于我们")
        case "意见反馈":
            print("点击了意见反馈")
        case "退出":
            print("点击了退出")
        default:
            break
        }
    }
    
    /// 设置UI界面
    fileprivate func setupUI() {
        // 设置信息视图背景色颜色
        leftUserInfoBGView.backgroundColor = UIColor.xl_colorWithHex(0x009ee7)
        // 设置圆角
        classIconImageV.layer.cornerRadius = iocnImageVHeight.constant * 0.5
        // 调整头像大小和间距
        if UIScreen.xl_ScreenW() <= 320 {
            iocnImageVHeight.constant = 75
            nameLMargin.constant = 20
        }
        // 设置按钮的字体颜色和高亮背景色
        for btn in leftButtonArray {
            // 设置标题颜色
            btn.setTitleColor(UIColor.xl_colorWithHex(0x666666), for: UIControlState())
            // 根据颜色创建背景图片
            let image = UIImage.xl_imageWithColor(UIColor.xl_colorWithHex(0xf0f0f0))
            // 设置背景图片
            btn.setBackgroundImage(image, for: .highlighted)
        }
        
        userScoreL.text = "400"
        // 获得保存的用户信息
        let score = Int("400") ?? 0
        // 判断分数比值
        var scoreRank: Int = 0
        switch score {
        case 0..<10:
            scoreRank = 0
        case 10..<50:
            scoreRank = 1
        case 50..<150:
            scoreRank = 2
        case 150..<350:
            scoreRank = 3
        case 350..<850:
            scoreRank = 4
        default:
            scoreRank = 5
        }
        
        // 添加视图
        for i in 1..<6 {
            let imageNmae = scoreRank >= i ? "jujube\(i)_light" : "jujube\(i)_dark"
            let imageV = UIImageView(image: UIImage(named: imageNmae))
            imageV.tag = 99 + i
            scoreView.addSubview(imageV)
        }
    }
    
    // 在视图显示完成后设置分数等级视图位置
    override func viewDidAppear(_ animated: Bool) {
        // 添加视图
        for iv in scoreView.subviews {
            // 根据背景图的宽度获得图片左边的距离
            let margin = scoreView.frame.width / 5
            // 计算x中心值
            let x = margin * CGFloat(iv.tag - 100) + iv.bounds.width/2
            // 设置约束
            iv.snp.makeConstraints({ (make) in
                make.centerX.equalTo(scoreView.snp.left).offset(x)
                make.bottom.equalTo(scoreView.snp.bottom).offset(-5)
            })
        }
    }
    
}
